//
//  KYRecentSearchView.m
//  Haizijiaoyuquan
//
//  Created by Kai Yu on 1/17/14.
//  Copyright (c) 2014 Zhang Kai Yu. All rights reserved.
//

#import "KYRecentSearchView.h"
#import "KYRecentSearchText.h"
#import "KYRecentSearchTextBasicBehavior.h"

#define RECENT_SEARCH_COLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@interface KYRecentSearchView () <UIDynamicAnimatorDelegate>

@end

@implementation KYRecentSearchView

- (UIDynamicAnimator *)animator
{
    if (!_animator) {
    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
        [_animator addBehavior:_behavior];
    }
    return _animator;
}

- (KYRecentSearchTextBasicBehavior *)behavior
{
    if (!_behavior) {
        _behavior = [[KYRecentSearchTextBasicBehavior alloc] init];
    }
    return _behavior;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
}

- (id)initWithFrame:(CGRect)frame
{
    return [self initWithRecentSearches:nil];
}

- (id)initWithRecentSearches:(NSArray *)recentSearches
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.recentSearches = recentSearches;
        self.animator.delegate = self;
        
//        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//        [self addGestureRecognizer:tap];
        
    }
    return self;
}




- (UIColor *)nextColor
{
    static NSInteger nextIndex;
    UIColor *color0 = [UIColor colorWithRed:0/255.0 green:184/255.0 blue:238/255.0 alpha:1.0];
    UIColor *color1 = [UIColor colorWithRed:232/255.0 green:64/255.0 blue:51/255.0 alpha:1.0];
    UIColor *color2 = [UIColor colorWithRed:131/255.0 green:191/255.0 blue:36/255.0 alpha:1.0];
    UIColor *color3 = [UIColor colorWithRed:224/255.0 green:77/255.0 blue:150/255.0 alpha:1.0];
    UIColor *color4 = RECENT_SEARCH_COLOR(118,117,118);
    UIColor *color5 = RECENT_SEARCH_COLOR(27,73,157);
    UIColor *color6 = RECENT_SEARCH_COLOR(88,191,197);
    UIColor *color7 = RECENT_SEARCH_COLOR(223,216,84);
    NSArray *colors = @[color0,color1,color2,color3,color4,color5,color6,color7];
    UIColor *returnColor = colors[nextIndex];
    nextIndex++;
    nextIndex = nextIndex%8;
    return returnColor;
}





- (void)animate
{
    static NSInteger count;
    static NSInteger index;
    static BOOL onceToken = NO;
    if (!onceToken) {
        count = [self.recentSearches count];
        onceToken = YES;
    }
    
    if (count > 0) {
        {
            KYRecentSearchText *text = [[KYRecentSearchText alloc] initWithText:self.recentSearches[index] color:[self nextColor]];
            [self addSubview:text];
            text.frame = [self rightFrameForIndex:count];
            [text sizeToFit];
            text.text=self.recentSearches[index];
            [self.behavior addItem:text];

            UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:text snapToPoint:[self centerPointForIndex:index]];
            snapBehavior.damping = ((double)arc4random() / 0x100000000);
            UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:text attachedToAnchor:[self centerPointForIndex:index]];
            attachmentBehavior.length = arc4random()%20;
            attachmentBehavior.frequency = ((double)arc4random() / 0x100000000);
            attachmentBehavior.damping = ((double)arc4random() / 0x100000000);
            [self.animator addBehavior:snapBehavior];
            [self.animator addBehavior:attachmentBehavior];
            text.snapBehavior = snapBehavior;
            text.attachmentBehavior = attachmentBehavior;
            count --;
            index ++;
            self.didConfiguredTextCallBack(text);
        }
        [self performSelector:@selector(animate) withObject:nil afterDelay:0.3];

    } else {
        onceToken = NO;
        index = 0;
    }

}

- (CGPoint)centerPointForIndex:(NSInteger)index
{
    return CGPointMake((0.5+index%4) * CGRectGetWidth(self.bounds)/4, (index / 4 + 0.5) * CGRectGetHeight(self.bounds)/2);
}

- (CGRect)rightFrameForIndex:(NSInteger)index
{
    return CGRectMake(CGRectGetMaxX(self.bounds) + index*CGRectGetWidth(self.bounds)/4+arc4random()%21, CGRectGetMinY(self.bounds) + index/4 * CGRectGetHeight(self.bounds)/2, CGRectGetWidth(self.bounds)/4, CGRectGetHeight(self.bounds)/2);
}

- (void)dynamicAnimatorWillResume:(UIDynamicAnimator*)animator
{
    
}
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator
{
    
}

@end
